﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Chat.Common;

public class ChattingPanelContent : UIBaseContent
{
    public ChattingPanelContent() : base(UIType.ChattingPanelContentType) { }
}

public class ChattingPanel : UIBaseView
{
    public GameObject ClientListPanel;
    public GameObject ClientListItemContent;
    public GameObject ClientListItemPrefab;
    public Text ChatNameText;
    /// <summary>
    /// 当前聊天面板的聊天对象
    /// </summary>
    public ChatObject ChatObject { get; set; }
    public ChatObject OriginChatObject { get; set; }
    /// <summary>
    /// 当前聊天面板的聊天类型
    /// </summary>
    public ChatType ChatType { get; set; }

    private bool isDisband = false;

    private void Awake()
    {
        Singleton<RoomManager>.GetInstance().OnKeyRemove += ChattingPanel_OnRoomRemove;
        Singleton<MemberManager>.GetInstance().OnKeyAdd += ChattingPanel_OnKeyAdd;
        Singleton<MemberManager>.GetInstance().OnKeyRemove += ChattingPanel_OnKeyRemove;
    }
    private void ShowMemberListPanel()
    {
        ClearMemberListPanel();
        InstantiateMemberListItem(ChatObject);
        foreach (var item in Singleton<MemberManager>.GetInstance().Dictionary)
            if(!item.Value.Equals(Singleton<Owner>.GetInstance()))
                InstantiateMemberListItem(item.Value);
    }
    private void ClearMemberListPanel()
    {
        foreach (Transform item in ClientListItemContent.transform)
            Destroy(item.gameObject);
    }
    private void InstantiateMemberListItem(ChatObject chatObject)
    {
        GameObject go = Instantiate(ClientListItemPrefab, ClientListItemContent.transform) as GameObject;
        go.GetComponent<Toggle>().group = ClientListItemContent.GetComponent<ToggleGroup>();
        go.GetComponent<MemberInfo>().chattingPanel = this;
        go.GetComponent<MemberInfo>().chatObject = chatObject;
    }
    private void ChattingPanel_OnKeyRemove(string obj)
    {
        Debug.Log("ChattingPanel_OnKeyRemove");

        foreach (Transform item in ClientListItemContent.transform)
        {
            if (item.gameObject.GetComponent<MemberInfo>().chatObject.Id.Equals(obj))
                Destroy(item.gameObject);
        }
    }
    private void ChattingPanel_OnKeyAdd(string obj)
    {
        Debug.Log("ChattingPanel_OnKeyAdd");
        InstantiateMemberListItem(Singleton<MemberManager>.GetInstance().GetMember(obj));
    }

    private void ChattingPanel_OnRoomRemove(string roomId)
    {
        if (roomId == ChatObject.Id && ChatType == ChatType.Public)
            isDisband = true;
    }


    // Update is called once per frame
    void Update()
    {
        if(ChatObject!=null)
        {
            ChatNameText.text = ChatObject.Name;
        }
        if(isDisband)
        {
            Singleton<ContentManager>.GetInstance().Push(new DisbandRoomPanelContent());
            isDisband = false;
        }
        
    }
    void ClearMessage()
    {
        MessageItem[] messageItems = GetComponentsInChildren<MessageItem>();
        foreach (var item in messageItems)
            Destroy(item.gameObject);
    }
    public void ExitRoom()
    {
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.ExitRoom);
        Singleton<ContentManager>.GetInstance().Pop();
    }
    public void OnBtnClientListClick()
    {
        if (ClientListPanel.activeInHierarchy)
            ClientListPanel.SetActive(false);
        else
        {
            Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetMemList);
            ClientListPanel.SetActive(true);
        }
    }
    public override void OnEnter(UIBaseContent uIContent)
    {
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetMemList);
        ClientListPanel.SetActive(false);
        ClearMessage();
        ClearMemberListPanel();
        ShowMemberListPanel();
        GetComponent<BaseCG>().Show();
    }
    public override void OnPause(UIBaseContent uIContent)
    {
        GetComponent<BaseCG>().Hide();
    }
    public override void OnResume(UIBaseContent uIContent)
    {
        GetComponent<BaseCG>().Show();
    }
    public override void OnExit(UIBaseContent uIContent)
    {
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetMemList);
        ClearMessage();
        ClearMemberListPanel();
        GetComponent<BaseCG>().Hide();
    }
    
}
